package com.vvlive.caibao.smartcity.ui;

import android.content.DialogInterface;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.adapter.FilesTypeAdapter;
import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.base.SuperAdapter;
import com.vvlive.caibao.smartcity.bean.FileDetails;
import com.vvlive.caibao.smartcity.utils.DialogUtil;
import com.vvlive.caibao.smartcity.view.CommonActionBar;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/11/18.
 * 导入会议文件
 * 音频，图片，视频，
 * txt，pdf，word，
 * 其他（浏览文件）
 * <p/>
 * 最近文件
 */
public class FilesInputActivity extends BaseActivity {

    private GridView mFileTypes;
    private ListView mFilesList;
    private FilesAdapter mAdapter;
    private List<File> mFiles = new ArrayList<>();

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_files_input;
    }

    @Override
    public void initWidget() {
        CommonActionBar actionBar = getCommonActionBar();
        actionBar.setTitle("资料导入");
        actionBar.setMoreVisible(false);

        mFileTypes = (GridView) this.findViewById(R.id.files_type);
        mFileTypes.setAdapter(new FilesTypeAdapter(this, getFileDetailsList()));


        mFilesList = (ListView) this.findViewById(R.id.files_recent);
        File[] files = FileUtil.getRootListFiles();
        log("_Files_", "RootFilePath = " + FileUtil.getRootFilePath());
        for (File file : files) {
            mFiles.add(file);
        }
        mAdapter = new FilesAdapter(mFiles);
        mFilesList.setAdapter(mAdapter);
//        ViewStorage.setListEmptyView(this, mFilesList, R.layout.view_no_files_tip);
        mFilesList.setOnItemClickListener(new InputClickListener());
    }

    private List<FileDetails> getFileDetailsList() {
        List<FileDetails> fileDetails = new ArrayList<>();
        fileDetails.add(new FileDetails("音频", 0, 3));
        fileDetails.add(new FileDetails("图片", 1, 5));
        fileDetails.add(new FileDetails("视频", 2, 1));
        fileDetails.add(new FileDetails("TXT", 3, 8));
        fileDetails.add(new FileDetails("PDF", 4, 2));
        fileDetails.add(new FileDetails("WORD", 5, 9));
        fileDetails.add(new FileDetails("其他", 6, 36));

        return fileDetails;
    }

    class InputClickListener implements DialogInterface.OnClickListener,
                             AdapterView.OnItemClickListener {

        private String mFileName;

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case DialogInterface.BUTTON_NEGATIVE:
                    dialog.dismiss();
                    break;
                case DialogInterface.BUTTON_POSITIVE:
                    toast("成功导入文件\"" + mFileName + "\"");
                    break;
            }
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final File currentFile = mFiles.get(position);
            if (currentFile.isFile()) {
                DialogUtil.showDialog(FilesInputActivity.this, "", "是否导入该文件？", this);
                mFileName = currentFile.getName();
            }
        }
    }

    /**
     * 重置文件列表的显示
     *
     * @param path 文件夹路径
     */
    private void resetList(String path) {
        if (!path.equals(FileUtil.getRootFilePath())) {
            File[] filesArray = FileUtil.getListFilesByPath(path);
            if (filesArray != null) {
                mFiles.clear();
                for (File f : filesArray) {
                    mFiles.add(f);
                }
                mAdapter.notifyDataSetChanged();
            }
        }/*else{
            finish();
        }*/
    }

    class FilesAdapter extends SuperAdapter<File> {

        private static final int FILE_ICON = R.mipmap.file_icn_input;
        private static final int FOLDER_ICON = R.mipmap.folder_icn;

        public FilesAdapter(List<File> dataList) {
            super(FilesInputActivity.this, R.layout.item_file_list, dataList);
        }

        @Override
        public void onBindData(ViewHolder viewHolder, File file, int position) {
            ImageView icon = viewHolder.get(R.id.file_icon);
            if (file.isFile()) {
                icon.setImageResource(FILE_ICON);
            } else if (file.isDirectory()) {
                icon.setImageResource(FOLDER_ICON);
            }
            ((TextView) viewHolder.get(R.id.file_name)).setText(file.getName());
        }
    }

    /**
     * 操作文件的工具类
     */
    private static class FileUtil {

        public static File rootFile = Environment.getExternalStorageDirectory();
        private static String path = rootFile.getAbsolutePath();

        public static File[] getListFilesByPath(String path) {
            return new File(path).listFiles();
        }

        private static void log(Object msg) {
            Log.i("_Files_", msg.toString());
        }

        public static File[] getRootListFiles() {
            return getListFilesByPath(path);
        }

        public static String addFolder(String folder) {
            path += "/" + folder;
            return path;
        }

        public static String getPath() {
            log("path = " + path);
            return path;
        }

        public static String goParent() {
            path = new File(path).getParent();
            return path;
        }

        public static String getRootFilePath() {
            return rootFile.getAbsolutePath();
        }

    }
}
